﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using System.Globalization;

namespace blackhouse.BlackApart.Business.Reservations {
	public class DaClient : IClient {

		#region Fields

		private ImpClient instance;
		private IReservationDataAccess dataAccess;

		#endregion

		#region Properties

		public int Id { get { return this.instance.Id; } private set { this.instance.Id = value; } }
		public string PhoneNumber { get { return this.instance.PhoneNumber; } set { this.instance.PhoneNumber = value; } }
		public string EMail { get { return this.instance.EMail; } set { this.instance.EMail = value; } }
		public string FirstName { get { return this.instance.FirstName; } set { this.instance.FirstName = value; } }
		public string LastName { get { return this.instance.LastName; } set { this.instance.LastName = value; } }
		public string PersonTitle { get { return this.instance.PersonTitle; } set { this.instance.PersonTitle = value; } }
        public string PrefferCurrency { get { return this.instance.PrefferCurrency; } set { this.instance.PrefferCurrency = value; } }
        public string PrefferLanguage { get { return this.instance.PrefferLanguage; } set { this.instance.PrefferLanguage = value; } }

		#endregion

		#region Constructors

		public DaClient(IReservationDataAccess dataAccess, int clientId) {
			this.dataAccess = dataAccess;
			if (clientId > 0) {
				ClientData data = this.dataAccess.GetClient(clientId);
				this.instance = this.ReadInstance(data);
			}
			if (this.instance == null)
				this.instance = new ImpClient();

		}

		private DaClient(IReservationDataAccess dataAccess, ClientData data) {
			this.dataAccess = dataAccess;
			this.instance = this.ReadInstance(data);
			if (this.instance == null)
				this.instance = new ImpClient();

		}

		#endregion

		#region Methods

		private ImpClient ReadInstance(ClientData data) {
			if (data.Id > 0) {
				ImpClient client = new ImpClient();
				client.Id = data.Id;
				client.EMail = data.EMail;
				client.PhoneNumber = data.PhoneNumber;
				client.FirstName = data.FirstName;
				client.LastName = data.LastName;
				client.PersonTitle = data.PersonTitle;
				client.ClientExists = true;
				client.PrefferCurrency = data.PrefferCurrency;
                client.PrefferLanguage = data.Language;
				return client;
			}
			else return null;
		}

		public IClient SearchClientByEmail(string email) {
			ClientData data = this.dataAccess.GetClientByEmail(email);
			return new DaClient(this.dataAccess, data);
		}

		public bool Exists() {
			return this.instance.Exists();
		}

		public bool Save() {
			if (this.instance.Save()) {
				this.Id = this.dataAccess.SaveClient(this.EMail, this.PhoneNumber, this.PersonTitle, this.FirstName, this.LastName, CultureInfo.CurrentCulture.Name, this.PrefferCurrency);
				this.instance.ClientExists = this.Id > 0;
				return this.instance.ClientExists;
			}
			else return false;
		}

		#endregion

	}
}
